package com.jxw.update;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jht.engine.platsign.SignUtil;
import com.jxw.constant.Constant;
import com.jxw.engine.platsign.MainActivity;
import com.jxw.launcher.MyApp;
import com.jxw.launcher.R;
import com.jxw.launcher.util.Logger;
import com.jxw.launcher.util.Util;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.TreeMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONException;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* loaded from: classes.dex */
public class Fragment5 extends Fragment {
    private static final String HOST = Util.API_HOST;
    private boolean isPing;
    private TextView mTextView;
    String mToken;
    private View mView;
    private AlertDialog netDialog;
    private String UPLOAD_File = HOST + "/api/jxwdevice/uploadLogFile";
    Handler mHandler = new Handler() { // from class: com.jxw.update.Fragment5.1
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            Fragment5.this.mTextView.setText(message.obj.toString());
        }
    };

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        this.mView = layoutInflater.inflate(R.layout.fragment_4, viewGroup, false);
        this.mTextView = (TextView) this.mView.findViewById(R.id.text);
        this.mToken = MyApp.getInstance().getSharedPreferences(MainActivity.PREFERENCE, 0).getString("token", "");
        if (TextUtils.isEmpty(this.mToken)) {
            login();
        } else {
            checkToekn(this.mToken);
        }
        this.isPing = false;
        if (haveNet()) {
            runPingIPprocess();
        }
        return this.mView;
    }

    @Override // android.support.v4.app.Fragment
    public void onHiddenChanged(boolean z) {
        super.onHiddenChanged(z);
        if (z || !haveNet() || this.isPing) {
            return;
        }
        runPingIPprocess();
    }

    public void onClick(View view) {
        runPingIPprocess();
    }

    public void runPingIPprocess() {
        new Thread() { // from class: com.jxw.update.Fragment5.2
            /* JADX WARN: Code restructure failed: missing block: B:23:0x0108, code lost:
                if (r2 != null) goto L23;
             */
            /* JADX WARN: Code restructure failed: missing block: B:32:0x012f, code lost:
                if (r2 == null) goto L24;
             */
            /* JADX WARN: Code restructure failed: missing block: B:34:0x0132, code lost:
                r1.append("\n");
                r1.append("ping api3.jiumentongbu.com\n");
                r3 = new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.Runtime.getRuntime().exec("ping -c 2 -w 10 api3.jiumentongbu.com").getInputStream()));
             */
            /* JADX WARN: Code restructure failed: missing block: B:35:0x0152, code lost:
                r4 = r3.readLine();
             */
            /* JADX WARN: Code restructure failed: missing block: B:36:0x0156, code lost:
                if (r4 == null) goto L29;
             */
            /* JADX WARN: Code restructure failed: missing block: B:37:0x0158, code lost:
                r1.append(r4 + "\n");
                r4 = r8.this$0.mHandler.obtainMessage();
                r4.obj = r1.toString();
                r8.this$0.mHandler.sendMessage(r4);
             */
            /* JADX WARN: Code restructure failed: missing block: B:41:0x0182, code lost:
                r3 = move-exception;
             */
            /* JADX WARN: Code restructure failed: missing block: B:42:0x0183, code lost:
                r1.append(r3.getMessage());
                r3 = r8.this$0.mHandler.obtainMessage();
                r3.obj = r1.toString();
                r8.this$0.mHandler.sendMessage(r3);
             */
            @Override // java.lang.Thread, java.lang.Runnable
            /*
                Code decompiled incorrectly, please refer to instructions dump.
                To view partially-correct code enable 'Show inconsistent code' option in preferences
            */
            public void run() {
                /*
                    Method dump skipped, instructions count: 507
                    To view this dump change 'Code comments level' option to 'DEBUG'
                */
                throw new UnsupportedOperationException("Method not decompiled: com.jxw.update.Fragment5.AnonymousClass2.run():void");
            }
        }.start();
    }

    public String macAddress() throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        String str = null;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface nextElement = networkInterfaces.nextElement();
            byte[] hardwareAddress = nextElement.getHardwareAddress();
            if (hardwareAddress != null && hardwareAddress.length != 0) {
                StringBuilder sb = new StringBuilder();
                int length = hardwareAddress.length;
                for (int i = 0; i < length; i++) {
                    sb.append(String.format("%02x:", Byte.valueOf(hardwareAddress[i])));
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                }
                String sb2 = sb.toString();
                Log.e("mac", "interfaceName=" + nextElement.getName() + ", mac=" + sb2);
                if (nextElement.getName().equals("wlan0")) {
                    Log.e("mac", " interfaceName =" + nextElement.getName() + ", mac=" + sb2);
                    str = sb2;
                }
            }
        }
        return str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void uploadFile(String str) {
        Log.e("zzj", "uploadFile");
        new OkHttpClient().newCall(new Request.Builder().url(this.UPLOAD_File).post(new FormBody.Builder().add("content", str).add("ext", ".txt").build()).addHeader("token", this.mToken).build()).enqueue(new Callback() { // from class: com.jxw.update.Fragment5.3
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                Log.e("zzj", "onFailure uploadFile=" + iOException.getMessage());
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Log.e("zzj", "uploadFile=" + string);
                if (response.isSuccessful()) {
                    try {
                        try {
                            new JSONObject().put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, new JSONObject(string).getJSONObject("data").getString(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } catch (JSONException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        });
    }

    public void login() {
        new Thread(new Runnable() { // from class: com.jxw.update.Fragment5.4
            @Override // java.lang.Runnable
            public void run() {
                final SharedPreferences sharedPreferences = MyApp.getInstance().getSharedPreferences(MainActivity.PREFERENCE, 0);
                String string = sharedPreferences.getString(MainActivity.CUSTOMISBN, "");
                String string2 = sharedPreferences.getString("series", "");
                String str = System.currentTimeMillis() + "";
                String genrateKey = SignUtil.genrateKey();
                TreeMap treeMap = new TreeMap();
                treeMap.put(MainActivity.APKISBN, string);
                treeMap.put("series", string2);
                treeMap.put("timestamp", str);
                treeMap.put("nonce", genrateKey);
                treeMap.put("appsecret", Constant.SIGN_KEY);
                String signature = SignUtil.signature(treeMap);
                Logger.getLogger().e("login signature=" + signature + ",items=" + treeMap);
                new OkHttpClient().newCall(new Request.Builder().url(Util.API_HOST + "/api/jxwuser/login?apkIsbn=" + string + "&series=" + string2 + "&timestamp=" + str + "&nonce=" + genrateKey + "&signature=" + signature).build()).enqueue(new Callback() { // from class: com.jxw.update.Fragment5.4.1
                    @Override // okhttp3.Callback
                    public void onFailure(Call call, IOException iOException) {
                        Log.d("zzj", "login onFailure=" + iOException.getMessage());
                    }

                    @Override // okhttp3.Callback
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d("zzj", "login =" + response);
                        if (response.isSuccessful()) {
                            try {
                                JSONObject jSONObject = new JSONObject(response.body().string());
                                Log.d("zzj", "json =" + jSONObject);
                                String string3 = jSONObject.getJSONObject("data").getString("token");
                                if (TextUtils.isEmpty(string3)) {
                                    return;
                                }
                                Fragment5.this.mToken = string3;
                                sharedPreferences.edit().putString("token", string3).apply();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        }).start();
    }

    public void checkToekn(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        new OkHttpClient().newCall(new Request.Builder().url(Util.API_HOST + "/api/jxwuser/checktoken?token=" + str).build()).enqueue(new Callback() { // from class: com.jxw.update.Fragment5.5
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                Log.d("zzj", "checkToekn onFailure=" + iOException.getMessage());
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("zzj", "checkToekn =" + response);
                if (response.isSuccessful()) {
                    try {
                        if (new JSONObject(response.body().string()).getInt("code") == 401) {
                            Fragment5.this.login();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void showNetDialog() {
        AlertDialog alertDialog = this.netDialog;
        if (alertDialog != null) {
            alertDialog.show();
            return;
        }
        this.netDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_Transparent).create();
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.dialog_nonet, (ViewGroup) null);
        inflate.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment5.6
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                Fragment5.this.netDialog.cancel();
            }
        });
        inflate.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.update.Fragment5.7
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                try {
                    Fragment5.this.startActivity(new Intent("android.settings.WIFI_SETTINGS"));
                    Fragment5.this.netDialog.cancel();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        this.netDialog.setView(inflate);
        this.netDialog.setCancelable(false);
        this.netDialog.setCanceledOnTouchOutside(false);
        this.netDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { // from class: com.jxw.update.Fragment5.8
            @Override // android.content.DialogInterface.OnKeyListener
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return i == 84;
            }
        });
        this.netDialog.show();
    }

    private boolean haveNet() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) getContext().getSystemService("connectivity")).getActiveNetworkInfo();
        if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable()) {
            showNetDialog();
            return false;
        }
        return true;
    }
}
